# coding=utf-8
"""
Author: Mr.夏末
@Function: 运行入口
"""

import os
from class22.ddt.excelddt import ddt
from common.Logger import path
from common.mail import Mail
from common.mysql import Mysql
from common.results import Results

os.system('rd /s/q result')
os.system('rd /s/q report')

#执行数据清理和数据构造
mysql = Mysql()
mysql.init_mysql()

#指定运行用例的名字
casename = 'HTTP接口用例.xlsx'

ddt.run_inter_cases(casename)

# 执行从结果文件生成报告的命令
os.system('allure generate result -o report --clean')

res = Results('result-' + casename)
res.get_summary()
print(res.get_summary())
res.summary['runtype'] = "Web"

mail = Mail()

html = mail.mail_info.get('html')

# 直接替换模板里的字符串
for key in res.summary.keys():
    if key == 'status':
        html = html.replace(key, str(res.summary.get(key)))
        if res.summary.get(key).upper() == "PASS":
            html = html.replace('scolor', 'green')
        else:
            html = html.replace('scolor', 'red')
    else:
        html = html.replace(key, str(res.summary.get(key)))

# 替换分组信息
tr = '<tr><td width="100" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="80" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;color:scolor">状态</td></tr>'
trs = ''
res.get_groups()
for line in res.group:
    tr1 = tr.replace('分组信息', line[0])
    tr1 = tr1.replace('用例总数', str(line[1]))
    tr1 = tr1.replace('通过数', str(line[2]))
    if line[3].upper() == "PASS":
        tr1 = tr1.replace('scolor','green')
    else:
        tr1 = tr1.replace('scolor','red')
    tr1 = tr1.replace('状态', line[3])
    trs += tr1

html = html.replace('mailbody',trs)
#在代码里面动态指定要发送的结果文件附件
mail.mail_info['mail_subject'] = casename.split('.')[0] + '自动化测试报告'
mail.mail_info['filenames'] = ["result-" + casename]
mail.mail_info['filepaths'] = [path + "lib/cases/result-" + casename]

mail.send(html)
